package com.cavalier.surtr.amop;

import com.cavalier.surtr.amop.provider.MessageSender;
import org.apache.commons.lang3.reflect.FieldUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.javamail.JavaMailSenderImpl;

import java.lang.reflect.Field;
import java.util.Date;


/**
 * Created by archangle on 17/3/6.
 */
@Configuration
public class AmopConfig {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;


    private static Logger logger = LoggerFactory.getLogger(MessageSender.class);

    @Bean
    @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    //回调确认必须设置为全局，因为不同的生产者需要不同的confirmCallback
    //单例模式的话，则所有的rabbitTemplate，实际的ConfirmCallback为最后一次申明的ConfirmCallback
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        RabbitTemplate template = new RabbitTemplate(connectionFactory);
        template.setMessageConverter(new Jackson2JsonMessageConverter());
        template.setReceiveTimeout(6000L);
        template.setEncoding("utf-8");
        return template;
    }

    @Bean
    public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(ConnectionFactory connectionFactory) {
        SimpleRabbitListenerContainerFactory listenerContainerFactory = new SimpleRabbitListenerContainerFactory();
        listenerContainerFactory.setConnectionFactory(connectionFactory);
        listenerContainerFactory.setMessageConverter(new Jackson2JsonMessageConverter());
        /**
         * 手动确认模式
         * 接受消息注意手动ack
         * */
        listenerContainerFactory.setAcknowledgeMode(AcknowledgeMode.MANUAL);
        listenerContainerFactory.setConcurrentConsumers(4);
        listenerContainerFactory.setErrorHandler(throwable -> {
            logger.error("handle MQ msg:  failed, record it to redis.", throwable);
            redisTemplate.opsForSet().add("MsgErr.MQ_MSG_ERR_RECORD_KEY",  new Date().toString());
        });
        return listenerContainerFactory;
    }

    @Bean
    public JavaMailSenderImpl javaMailSender() {
        JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
        javaMailSender.setHost("stmp.qq.com");
        javaMailSender.setDefaultEncoding("utf-8");
        return javaMailSender;
    }

}
